<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item :label="$t('writeofftime')" class="width100">
              <el-radio-group
                v-model="tableFrom.date"
                type="button"
                class="mr20"
                size="small"
                clearable
                @change="selectChange(tableFrom.date)"
              >
                <el-radio-button
                  v-for="(item, i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                  >{{ item.text }}</el-radio-button
                >
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('customtime')"
                style="width: 250px"
                clearable
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item :label="$t('ordernumber')" class="width100">
              <el-input
                v-model="tableFrom.keywords"
                :placeholder="$t('please44')"
                class="selWidth"
                size="small"
                clearable
                @keyup.enter.native="getList(1), getCardList()"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1), getCardList()"
                />
              </el-input>
              <!-- <el-button size="small" type="primary" @click="exportOrder">{{ $t('listexport') }}</el-button>
              <el-button size="small" type="primary" @click="getExportFileList">{{ $t('exportrecords') }}</el-button> -->
            </el-form-item>
            <el-form-item :label="$t('userinformation')" class="width100">
              <el-input
                v-model="tableFrom.username"
                :placeholder="$t('please110')"
                class="selWidth"
                size="small"
                @keyup.enter.native="getList(1), getCardList()"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1), getCardList()"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <cards-data :card-lists="cardLists" />
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item :label="$t('totalpriceofgoods')">
                <span>{{ props.row.total_price | filterEmpty }}</span>
              </el-form-item>
              <el-form-item :label="$t('ordertime')">
                <span>{{ props.row.create_time | filterEmpty }}</span>
              </el-form-item>
              <el-form-item :label="$t('usernotes')">
                <span>{{ props.row.mark | filterEmpty }}</span>
              </el-form-item>
              <el-form-item :label="$t('merchantremarks')">
                <span>{{ props.row.remark | filterEmpty }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column :label="$t('orderno')" min-width="170">
          <template slot-scope="scope">
            <span style="display: block" v-text="scope.row.order_sn" />
            <span
              v-show="scope.row.is_del > 0"
              style="color: #ed4014; display: block"
              >{{ $t("userdeleted") }}</span
            >
          </template>
        </el-table-column>
        <el-table-column :label="$t('ordertype')" min-width="170">
          <template slot-scope="scope">
            <span>{{
              scope.row.order_type == 0
                ? $t("generalorder")
                : $t("writeofforders")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="real_name"
          :label="$t('consignee')"
          min-width="130"
        />
        <el-table-column :label="$t('productinformation')" min-width="330">
          <template slot-scope="scope">
            <div
              v-for="(val, i) in scope.row.orderProduct"
              :key="i"
              class="tabBox acea-row row-middle"
            >
              <div class="demo-image__preview">
                <el-image
                  :src="val.cart_info.product.image"
                  :preview-src-list="[val.cart_info.product.image]"
                />
              </div>
              <span class="tabBox_tit"
                >{{ val.cart_info.product.store_name + " | "
                }}{{ val.cart_info.productAttr.sku }}</span
              >
              <span class="tabBox_pice">{{
                "$" + val.cart_info.productAttr.price + " x " + val.product_num
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="pay_price"
          :label="$t('actualpayment')"
          min-width="100"
        />
        <el-table-column
          prop="pay_price"
          :label="$t('writeoffclerk')"
          min-width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.paid">{{
              scope.row.verifyService
                ? scope.row.verifyService.nickname
                : $t("writeoffbyadministrator")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="serviceScore"
          :label="$t('writeoffstatus')"
          min-width="80"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.status >= 2 ? $t("writtenoff") : $t("unwrittenoff")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="verify_time"
          :label="$t('writeofftime')"
          min-width="150"
        />
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--导出订单列表-->
    <file-list ref="exportList" />
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  takeOrderListApi,
  takeChartApi,
  exportOrderApi,
  takeCardListApi,
} from "@/api/order";
import fileList from "@/components/exportFile/fileList";
import cardsData from "@/components/cards/index";
export default {
  components: { cardsData, fileList },
  data() {
    return {
      orderId: 0,
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        order_sn: "",
        status: "",
        date: "",
        page: 1,
        limit: 20,
        type: "4",
        order_type: "1",
        username: "",
        keywords: "",
      },
      orderChartType: {},
      timeVal: [],
      fromList: {
        title: this.$t("selectionperiod"),
        custom: true,
        fromTxt: [
          { text: this.$t("all"), val: "" },
          { text: this.$t("today"), val: "today" },
          { text: this.$t("yesterday"), val: "yesterday" },
          { text: this.$t("last7days"), val: "lately7" },
          { text: this.$t("last30days"), val: "lately30" },
          { text: this.$t("thismonth"), val: "month" },
          { text: this.$t("thisyear"), val: "year" },
        ],
      },
      selectionList: [],
      ids: "",
      tableFromLog: {
        page: 1,
        limit: 10,
      },
      tableDataLog: {
        data: [],
        total: 0,
      },
      LogLoading: false,
      dialogVisible: false,
      fileVisible: false,
      cardLists: [],
      orderDatalist: null,
      headeNum: [
        { type: 1, name: this.$t("all"), count: 10 },
        { type: 2, name: this.$t("generalorder"), count: 3 },
        { type: 3, name: this.$t("liveorder"), count: 1 },
        { type: 4, name: this.$t("writeofforders"), count: 2 },
        { type: 5, name: this.$t("grouporder"), count: 0 },
        { type: 6, name: this.$t("secondkillorder"), count: 6 },
        { type: 7, name: this.$t("bargainorder"), count: 5 },
      ],
    };
  },
  mounted() {
    this.headerList();
    this.getCardList();
    this.getList(1);
  },
  methods: {
    // 导出
    exportOrder() {
      exportOrderApi({
        status: this.tableFrom.status,
        date: this.tableFrom.date,
        take_order: 1,
      })
        .then((res) => {
          /* this.$message.success(res.message);
          this.fileVisible = true;
          this.$refs.exportList.exportFileList();*/
          const h = this.$createElement;
          this.$msgbox({
            title: this.$t("tips"),
            message: h("p", null, [
              h("span", null, this.$t("please71")),
              h("span", { style: "color: teal" }, this.$t("exportrecords")),
              h("span", null, this.$t("see")),
            ]),
            confirmButtonText: this.$t("igotit"),
          }).then((action) => {});
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    getExportFileList() {
      this.fileVisible = true;
      this.$refs.exportList.exportFileList("order");
    },

    pageChangeLog(page) {
      this.tableFromLog.page = page;
      this.getList("");
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val;
      this.getList("");
    },
    handleSelectionChange(val) {
      this.selectionList = val;
      const data = [];
      this.selectionList.map((item) => {
        data.push(item.id);
      });
      this.ids = data.join(",");
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.date = tab;
      this.getCardList();
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.getCardList();
      this.getList(1);
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      takeOrderListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          //   this.cardLists = res.data.stat
          this.listLoading = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.listLoading = false;
        });
    },
    getCardList() {
      takeCardListApi(this.tableFrom)
        .then((res) => {
          this.cardLists = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
    headerList() {
      takeChartApi()
        .then((res) => {
          this.orderChartType = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
</style>
